﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using BLL;
using BOL;
using DevComponents.DotNetBar;
using DevComponents.AdvTree;
using System.IO;
using System.Data.SqlClient;

namespace QuanLyHangXeKhach
{
    public partial class frmCapnhatKhachHang : UserControl
    {
        CapnhatKhachHang cnkh = new CapnhatKhachHang();
        DieuKhien dk = new DieuKhien();

        public frmCapnhatKhachHang()
        {
            InitializeComponent();
        }

        #region Điều khiển đóng/mở form
        private DieuKhien _Commands;
        /// <summary>
        /// Gets or sets the commands associated with the control.
        /// </summary>
        public DieuKhien Commands
        {
            get { return _Commands; }
            set
            {
                if (value != _Commands)
                {
                    DieuKhien oldValue = _Commands;
                    _Commands = value;
                    OnCommandsChanged(oldValue, value);
                }
            }
        }
        /// <summary>
        /// Called when Commands property has changed.
        /// </summary>
        /// <param name="oldValue">Old property value</param>
        /// <param name="newValue">New property value</param>
        protected virtual void OnCommandsChanged(DieuKhien oldValue, DieuKhien newValue)
        {
            if (newValue != null)
            {
                btndong.Command = newValue.dongfrmcnkh;                
            }
            else
            {
                btndong.Command = null;                
            }
        }
        #endregion

        private void Loadfrm()
        {

            List<khachhang> nvpt = cnkh.laydskhachhang("2");
            advTree1.Nodes.Clear();
            foreach (var row in nvpt)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.makh, row.tenkh,row.gioitinhkh, row.ngaysinhkh, row.diachikh, row.sdtkh));
            }
        }

        private Node CreatenoidenRow(string ma, string ten, string giotinh, string ngay, string diachi,  string sdt)
        {
            Node node = new Node(ma);
            node.Cells.Add(new Cell(ten));
            node.Cells.Add(new Cell(giotinh));
            node.Cells.Add(new Cell(ngay));
            node.Cells.Add(new Cell(diachi));            
            node.Cells.Add(new Cell(sdt));
            return node;
        }

        private void frmCapnhatKhachHang_Load(object sender, EventArgs e)
        {
            Loadfrm();
            txttimkiem.WatermarkText = "Tìm kiếm";
            cobgioitinh.Items.Add("Nam");
            cobgioitinh.Items.Add("Nữ");
            cobgioitinh.Items.Add("Khác");
        }

        private void advTree1_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e)
        {
            try
            {
                Node node = e.Node;
                txtmakh.Text = node.Cells[0].Text;
                txttenkh.Text = node.Cells[1].Text;
                cobgioitinh.Text = node.Cells[2].Text;
                maskngaysinh.Text = node.Cells[3].Text.Replace("/","");
                txtdiachi.Text = node.Cells[4].Text;               
                txtsdt.Text = node.Cells[5].Text;
               
            }
            catch (Exception) { }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            List<khachhang> nvpt = cnkh.laydskhachhang(txttimkiem.Text);
            advTree1.Nodes.Clear();
            foreach (var row in nvpt)
            {
                advTree1.Nodes.Add(CreatenoidenRow(row.makh, row.tenkh, row.gioitinhkh, row.ngaysinhkh, row.diachikh, row.sdtkh));
            }
        }
        public void xoatrang()
        {
            txtmakh.Text = "";
            txttenkh.Text = "";
            cobgioitinh.Text = "";
            txtdiachi.Text = "";
            maskngaysinh.Text = "";
            txtsdt.Text = "";
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            btnsua.Text = "Lưu";
            btnxoa.Text = "Hủy bỏ";
            btnthem.Enabled = false;
            xoatrang();
            txtmakh.Text = dk.NextID(cnkh.makhlonnhat(), "KH");
            this.advTree1.AfterNodeSelect -= new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txttenkh.Text == "" || txtsdt.Text == "" || txtdiachi.Text == "" || cobgioitinh.Text == "" || maskngaysinh.Text == "")
                MessageBoxEx.Show("Nhập đủ thông tin trước khi tiếp tục", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                if (btnsua.Text == "Lưu")
                {

                    cnkh.themkh(txtmakh.Text, txttenkh.Text, cobgioitinh.Text, dk.catngay(maskngaysinh.Text), txtdiachi.Text, txtsdt.Text);
                    MessageBoxEx.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    xoatrang();
                    txtmakh.Text = dk.NextID(cnkh.makhlonnhat(), "KH");
                    Loadfrm();

                }
                else
                {

                    cnkh.suakh(txtmakh.Text, txttenkh.Text, cobgioitinh.Text, dk.catngay(maskngaysinh.Text), txtdiachi.Text, txtsdt.Text);
                    MessageBoxEx.Show("Sửa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadfrm();
                }
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (btnxoa.Text == "Hủy bỏ")
            {
                btnthem.Enabled = true;
                btnsua.Text = "Sửa";
                btnxoa.Text = "Xóa";
                xoatrang();
                this.advTree1.AfterNodeSelect += new DevComponents.AdvTree.AdvTreeNodeEventHandler(this.advTree1_AfterNodeSelect);
            }
            else
            {
                if (txtmakh.Text == "")
                    MessageBoxEx.Show("Chọn đối tượng cần xóa ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    DialogResult dr = MessageBoxEx.Show("Bạn muốn xóa khách hàng " + txttenkh.Text + " này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    if (dr == DialogResult.Yes)
                    {
                        cnkh.xoakh(txtmakh.Text);
                        MessageBoxEx.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        xoatrang();
                        Loadfrm();
                    }
                }
            }
        }

        private void labelX5_Click(object sender, EventArgs e)
        {

        }

        private void btndong_Click(object sender, EventArgs e)
        {
           
        }

        private void maskngaysinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}



